package OSdesign.Function_show;

import java.sql.SQLException;

public class PlayerManager {
    public PlayerManager(){
        //构造函数
    }
    public boolean addGrade(String[] strs){
        boolean done = false;
        //连接数据库
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        System.out.println("正在执行SQL语句");
        String sql = "insert into grade values('"+strs[0]+"','"+strs[1]+"','"+strs[2]+"','"+strs[3]+"','"+Integer.valueOf(strs[4])+"')";
        try {
            mysql.stmt.execute(sql);
            done=true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return done;
    }
    public Object[][] rankGrade_run(String str){
        int i = 0;
        int count = 0;
        int ranking = 1;
        int flag = 0;
        //连接数据库
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        System.out.println("正在执行SQL语句");
        String sql1 = "select pno,mno,score from grade where mno in(select mno from competition where mname='"+str+"') order by score";

        try {
            mysql.set = mysql.stmt.executeQuery(sql1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next())  break;
                count++;
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        Object[][] objects = new Object[count][4];
        System.out.println(count);
        try{
            mysql.set = mysql.stmt.executeQuery(sql1);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try {
            while(true){
                if(!mysql.set.next()) break;
                System.out.println("正在逐条执行SQL语句");
                objects[i][0] = ranking+"";
                objects[i][1] = mysql.set.getString("pno");
                objects[i][2] = mysql.set.getString("mno");
                objects[i][3] = mysql.set.getString("score");
                i++;  ranking++;
            }
            System.out.println("逐条执行完毕");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return objects;
    }
    public Object[][] rankGrade_unrun(String str){
        int i = 0;
        int count = 0;
        int ranking = 1;
        int flag = 0;
        //连接数据库
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        System.out.println("正在执行SQL语句");
        String sql2 = "select pno,mno,score from grade where mno in(select mno from competition where mname='"+str+"') order by score desc";
        try {
            mysql.set = mysql.stmt.executeQuery(sql2);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next())  break;
                count++;
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        Object[][] objects = new Object[count][4];
        System.out.println(count);
        try {
            while(true){
                if(!mysql.set.next()) break;
                System.out.println("正在逐条执行SQL语句");
                objects[i][0] = ranking+"";
                objects[i][1] = mysql.set.getString("pno");
                objects[i][2] = mysql.set.getString("mno");
                objects[i][3] = mysql.set.getString("score");
                i++;  ranking++;
            }
            System.out.println("逐条执行完毕");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return objects;
    }

}
